package com.ljm.shiro.util;

import com.ljm.common.constants.ShiroConstants;
import com.ljm.common.util.SpringBeanUtil;
import com.ljm.common.util.StringUtil;
import com.ljm.sys.auth.api.SysAuthPermissionApi;
import com.ljm.sys.auth.api.SysAuthRoleApi;
import com.ljm.sys.org.api.SysOrgUserApi;
import com.ljm.sys.org.dto.SysOrgUserDto;
import org.apache.shiro.SecurityUtils;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 版权：    Copyright by ljm
 * 描述：
 * 修改人：  HuamingChen
 * 修改时间：2018/12/21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
public class UserUtil {
    /**
     * 用户接口
     */
    private static SysOrgUserApi sysUserApi=null;
    /**
     * 角色接口
     */
    private static SysAuthRoleApi sysRoleApi=null;
    /**
     * 权限接口
     */
    private static SysAuthPermissionApi sysPermissionApi=null;

    private static SysOrgUserApi getSysUserApi() {
        return sysUserApi==null?SpringBeanUtil.getBean(SysOrgUserApi.class):sysUserApi;
    }

    private static SysAuthRoleApi getSysRoleApi() {
        return sysRoleApi==null?SpringBeanUtil.getBean(SysAuthRoleApi.class):sysRoleApi;
    }

    private static SysAuthPermissionApi getSysPermissionApi() {
        return sysPermissionApi==null?SpringBeanUtil.getBean(SysAuthPermissionApi.class):sysPermissionApi;
    }

    public static SysOrgUserDto getUserInfo(){
        //获取当前登录用户
        String userName=SecurityUtils.getSubject().getPrincipal().toString();
        return getUser(userName);
    }

    /**
     * 获取用户角色权限
     * @param userId 用户id
     * @return 用户
     */
    private static Map<String,Set<String>> getRoleAndPermission(Long userId){
        Map<String,Set<String>> roleAndPermission=new HashMap<>();
        // 查询角色信息
        Set<String> roles = getSysRoleApi().getUserRoles(userId);
        if (!StringUtil.isEmpty(roles)) {
            roleAndPermission.put(ShiroConstants.CURRENT_ROLES,roles);
        }
        // 查询权限
        Set<String> permisstions = getSysPermissionApi().getUserPermission(userId);
        if (!StringUtil.isEmpty(permisstions)) {
            roleAndPermission.put(ShiroConstants.CURRENT_PERMISSIONS,permisstions);
        }
        return roleAndPermission;
    }

    /**
     * 获取用户信息
     * @param userName 用户名称
     * @return 用户信息
     */
    public static SysOrgUserDto getUser(String userName){
        // 获取登录用户
        Object userObj=SecurityUtils.getSubject().getSession().getAttribute(ShiroConstants.CURRENT_USER);
        if(userObj!=null){
            if(userObj instanceof SysOrgUserDto){
                return (SysOrgUserDto) userObj;
            }
        }
        SysOrgUserDto userInfo = getSysUserApi().getUserByUserName(userName);
        Map<String,Set<String>> roleAndPermission=getRoleAndPermission(userInfo.getId());
        Set<String> roles=roleAndPermission.get(ShiroConstants.CURRENT_ROLES);
        if(!CollectionUtils.isEmpty(roles)){
            userInfo.setRoles(roles);
        }
        Set<String> permissions=roleAndPermission.get(ShiroConstants.CURRENT_PERMISSIONS);
        if(!CollectionUtils.isEmpty(permissions)){
            userInfo.setPermissions(permissions);
        }
        //加入缓存
        SecurityUtils.getSubject().getSession().setAttribute(ShiroConstants.CURRENT_USER,userInfo);
        return userInfo;
    }
}
